import { Injectable } from '@nestjs/common';

import * as crypto from 'crypto';

@Injectable()
export class AppService {
  getToken(method, res, accessKey, et) {
    const version = '2022-05-01';
    et = Math.ceil((Date.now() + et) / 1000); // token有效时间
    const base64Key = Buffer.from(accessKey, 'base64'); // accessKey base64编码
    const StringForSignature = et + '\n' + method + '\n' + res + '\n' + version;
    const sign = encodeURIComponent(crypto.createHmac(method, base64Key).update(StringForSignature).digest('base64'));
    const encodeRes = encodeURIComponent(res);

    return {
      code: 0,
      message: 'success',
      data: `version=${version}&res=${encodeRes}&et=${et}&method=${method}&sign=${sign}`
    };
  }
}
